/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telas;

import connection.EMF;
import dao.DAOGenerico;
import java.awt.Color;
import model.Fornecedor;

/**
 *
 * @author Dentinho
 */
public class CadastroFornecedor extends javax.swing.JDialog {
    private Fornecedor fornecedor;

    /**
     * Creates new form CadastroFornecedor
     */
    public CadastroFornecedor(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    CadastroFornecedor() {
        initComponents();
        desativaCampos();
    }
    
    
    
    private void initAction() {
       fornecedor = new Fornecedor();
    }
    
    private void vazio(){
        
    }
    
    private void limpaCampos(){
        textCep.setText("");
        textCidade.setText("");
        textCnpj.setText("");
        textEndereco.setText("");
        textFantasia.setText("");
        textInsEstadual.setText("");
        textRazao.setText("");
        textTelefone.setText("");
    }
    private void ativaCampos(){
        textCep.setEnabled(true);
        textCidade.setEnabled(true);
        textCnpj.setEnabled(true);
        textEndereco.setEnabled(true);
        textFantasia.setEnabled(true);
        textInsEstadual.setEnabled(true);
        textRazao.setEnabled(true);
        textTelefone.setEnabled(true);
        jComboBox1.setEnabled(true);
        btSalvar.setEnabled(true);
        btLimpar.setEnabled(true);
        btNovo.setEnabled(false);
    }
    private void desativaCampos(){
        textCep.setEnabled(false);
        textCidade.setEnabled(false);
        textCnpj.setEnabled(false);
        textEndereco.setEnabled(false);
        textFantasia.setEnabled(false);
        textInsEstadual.setEnabled(false);
        textRazao.setEnabled(false);
        textTelefone.setEnabled(false);
        jComboBox1.setEnabled(false);
        btNovo.setEnabled(true);
        btSalvar.setEnabled(false);
        btLimpar.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btSalvar = new javax.swing.JButton();
        btNovo = new javax.swing.JButton();
        painel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        textRazao = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        textFantasia = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        textTelefone = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        textCep = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        textEndereco = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        textCidade = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        lbEros = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        textCnpj = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        textInsEstadual = new javax.swing.JTextField();
        btLimpar = new javax.swing.JButton();
        btCancela = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro Fornecedor");

        btSalvar.setText("Salvar");
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        btNovo.setText("Novo");
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });

        painel.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Razão Social");

        jLabel2.setText("Nome Fantasia");

        jLabel3.setText("Telefone");

        jLabel4.setText("Cep");

        jLabel6.setText("Endereço");

        jLabel7.setText("Cidade");

        jLabel8.setText("Estado");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Acre", "Alagoas", "Amazonas", "Amapá", "Bahia", "Ceará", "Distrito Federal", "Espírito Santo", "Goiás", "Maranhão", "Minas Gerais", "Mato Grosso do Sul", "Mato Grosso", "Pará", "Paraíba", "Pernambuco", "Piauí", "Paraná", "Rio de Janeiro", "Rio Grande do Norte", "Rondônia", "Rondonia", "Rio Grande do Sul", "Santa Catarina", "Sergipe", "São Paulo", "Tocantins" }));

        lbEros.setText("*");

        jLabel5.setText("CNPJ");

        jLabel9.setText("Inscrição Estadual");

        javax.swing.GroupLayout painelLayout = new javax.swing.GroupLayout(painel);
        painel.setLayout(painelLayout);
        painelLayout.setHorizontalGroup(
            painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, painelLayout.createSequentialGroup()
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(painelLayout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(70, 70, 70)
                        .addComponent(textCidade))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, painelLayout.createSequentialGroup()
                        .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel5)
                            .addComponent(jLabel9)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6))
                        .addGap(16, 16, 16)
                        .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textTelefone)
                            .addComponent(textFantasia)
                            .addComponent(textRazao)
                            .addComponent(textCnpj)
                            .addComponent(textInsEstadual)
                            .addComponent(textCep)
                            .addComponent(textEndereco, javax.swing.GroupLayout.Alignment.TRAILING)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, painelLayout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addGap(70, 70, 70)
                        .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(painelLayout.createSequentialGroup()
                                .addComponent(lbEros)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jComboBox1, 0, 320, Short.MAX_VALUE))))
                .addGap(41, 41, 41))
        );
        painelLayout.setVerticalGroup(
            painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(textRazao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(textFantasia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(textInsEstadual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textCep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lbEros)
                .addGap(19, 19, 19))
        );

        btLimpar.setText("Limpar");
        btLimpar.setFocusable(false);
        btLimpar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btLimpar.setName("btLimpar"); // NOI18N
        btLimpar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLimparActionPerformed(evt);
            }
        });

        btCancela.setText("Cancelar e Sair");
        btCancela.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btSalvar)
                        .addGap(18, 18, 18)
                        .addComponent(btLimpar)
                        .addGap(15, 15, 15)
                        .addComponent(btNovo)
                        .addGap(18, 18, 18)
                        .addComponent(btCancela))
                    .addComponent(painel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(73, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btLimpar)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btSalvar)
                        .addComponent(btNovo)
                        .addComponent(btCancela)))
                .addContainerGap(52, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        initAction();
        vazio();
        int i = 0;
        if ("".equals(textRazao.getText())) {
            i = 1;
            textRazao.setBackground(Color.red);
        }
        if ("".equals(textFantasia.getText())) {
            i = 1;
            textFantasia.setBackground(Color.red);
        }
        if ("".equals(textTelefone.getText())) {
            i = 1;
            textTelefone.setBackground(Color.red);
        }
        if ("".equals(textEndereco.getText())) {
            i = 1;
            textEndereco.setBackground(Color.red);
        }
        if ("".equals(textCidade.getText())) {
            i = 1;
            textCidade.setBackground(Color.red);
        }
        if (i == 1) {
            lbEros.setText("Campos em destaque não podem ser nulos!");
            lbEros.setForeground(Color.red);
        } else {
            DAOGenerico<Fornecedor> dao = new DAOGenerico<>(EMF.getEm(), Fornecedor.class);
            fornecedor.setRazaoSocial(textRazao.getText());
            fornecedor.setCnpj(textCnpj.getText());
            fornecedor.setNomeFantasia(textFantasia.getText());
            fornecedor.setCep(textCep.getText());
            fornecedor.setCidade(textCidade.getText());
            fornecedor.setInscEstadual(textInsEstadual.getText());
            fornecedor.setEndereco(textEndereco.getText());
            fornecedor.setEstado(jComboBox1.getSelectedItem().toString());
            fornecedor.setTelefone(textTelefone.getText());
            dao.salvar(fornecedor);
            limpaCampos();
            desativaCampos();
        }
    }//GEN-LAST:event_btSalvarActionPerformed

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        painel.setEnabled(true);
        limpaCampos();
        ativaCampos();
    }//GEN-LAST:event_btNovoActionPerformed

    private void btLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLimparActionPerformed
        limpaCampos();
    }//GEN-LAST:event_btLimparActionPerformed

    private void btCancelaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelaActionPerformed
        dispose();
    }//GEN-LAST:event_btCancelaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CadastroFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CadastroFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CadastroFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CadastroFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                CadastroFornecedor dialog = new CadastroFornecedor(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancela;
    private javax.swing.JButton btLimpar;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btSalvar;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel lbEros;
    private javax.swing.JPanel painel;
    private javax.swing.JTextField textCep;
    private javax.swing.JTextField textCidade;
    private javax.swing.JTextField textCnpj;
    private javax.swing.JTextField textEndereco;
    private javax.swing.JTextField textFantasia;
    private javax.swing.JTextField textInsEstadual;
    private javax.swing.JTextField textRazao;
    private javax.swing.JTextField textTelefone;
    // End of variables declaration//GEN-END:variables

    
}
